package com.woyou.ui.adapter;

import java.util.List;

import com.citaq.ideliver.R;
import com.squareup.picasso.Picasso;
import com.woyou.bean.CommentsCon;
import com.woyou.model.UserModel;
import com.woyou.model.UserModel_;
import com.woyou.ui.component.CircleImageView;
import com.woyou.utils.LogUtil;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * 店铺评论的适配器
 * 
 * @author shenzhou 2014年12月25日
 */
public class ShopCommentAdapter extends BaseAdapter {
	public static final String TAG = "ShopCommentAdapter";
	private Drawable defDrawable;
	private Context mContext;
	private List<CommentsCon> mList;
	private UserModel mUserController;
	
	public ShopCommentAdapter(Context mContext, List<CommentsCon> mList) {
		super();
		this.mContext = mContext;
		this.mList = mList;
		Resources res = mContext.getResources();
		Bitmap bitmap = BitmapFactory.decodeResource(res, R.raw.touxiang);
		defDrawable = new BitmapDrawable(res, bitmap);
		mUserController = UserModel_.getInstance_(mContext);
	}

	@Override
	public int getCount() {
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = convertView;
		ViewHolder holder;
		if ( view == null ) {
			view = View.inflate(mContext, R.layout.item_comment_lv, null);
			holder = new ViewHolder();
			holder.uPic = (CircleImageView) view.findViewById(R.id.comment_image);
			holder.uName = (TextView) view.findViewById(R.id.comment_name);
			holder.uLevel = (TextView) view.findViewById(R.id.comment_times);
			holder.cTime = (TextView) view.findViewById(R.id.comment_date);
			holder.comments = (TextView) view.findViewById(R.id.comment_info);
			holder.grade = (ImageView) view.findViewById(R.id.comment_grade);
			holder.sendTimes = (TextView) view.findViewById(R.id.comment_sendtimes);
			holder.layout = (RelativeLayout) view.findViewById(R.id.comment_rela);
			holder.retime = (TextView) view.findViewById(R.id.comment_retimes);
			holder.reComments = (TextView) view.findViewById(R.id.comment_recomment);
			view.setTag(holder);
		} else {
			holder = (ViewHolder) view.getTag();
		}
		CommentsCon commentsCon = mList.get(position);
		LogUtil.i(TAG, "commentsCon:" + commentsCon);
		
		holder.uPic.setImageDrawable(defDrawable);
		if( !TextUtils.isEmpty(commentsCon.getuPic()) ) {
			Picasso.with(mContext).load(commentsCon.getuPic()).into(holder.uPic);
		}
		
		//加载用户姓名
		if ( !TextUtils.isEmpty(commentsCon.getuName()) ) {
			holder.uName.setText(commentsCon.getuName());
		} else {
			if ( mUserController.getUserInfo()!=null ){
				holder.uName.setText(mUserController.getUserInfo().getuId());
			}
		}
		
		//加载用户等级
//		if ( 0 != commentsCon.getuLevel() ) {
//			if (1 == commentsCon.getuLevel()) {
//				holder.uLevel.setText("首次下单");
//			}
//			else if (2 == commentsCon.getuLevel()) {
//				holder.uLevel.setText("资深熟客");
//			}
//			else if (3 == commentsCon.getuLevel()) {
//				holder.uLevel.setText("VIP");
//			}
//		}
		
		//加载评论时间
		if ( !TextUtils.isEmpty(commentsCon.getcTime()) ) {
			holder.cTime.setText(commentsCon.getcTime());
		} else {
			holder.cTime.setText("");
		}
		
		//加载评论内容
		if ( !TextUtils.isEmpty(commentsCon.getComments()) ) {
			holder.comments.setVisibility(View.VISIBLE);
			holder.comments.setText(commentsCon.getComments());
		}else{
			holder.comments.setText("");
			holder.comments.setVisibility(View.GONE);
		}
		
		//加载评论等级
		setScoreLevel(commentsCon.getScore(), holder.grade);
		
		//加载送达时间
		String time = commentsCon.getDeliveryTime();
		if ( !TextUtils.isEmpty(time) ) {
			int ct = 0;
			try {
				ct = Integer.parseInt(time);
			} catch (Exception e) {
				LogUtil.d(TAG, "NumberFormatException");
				ct = 0;
			}
			if ( ct>180 ) {
				holder.sendTimes.setText("");
				holder.sendTimes.setVisibility(View.GONE);
			} else {
				holder.sendTimes.setVisibility(View.VISIBLE);
				holder.sendTimes.setText(commentsCon.getDeliveryTime() + "分钟送达");
			}
		}else{
			holder.sendTimes.setText("");
			holder.sendTimes.setVisibility(View.GONE);
		}
		
		//加载掌柜回复内容
		if ( !TextUtils.isEmpty(commentsCon.getReComments()) ) {
			holder.layout.setVisibility(View.VISIBLE);
			holder.reComments.setText(commentsCon.getReComments());
		} else {
			holder.reComments.setText("");
			holder.layout.setVisibility(View.GONE);
		}
		
		//加载掌柜回复时间
		if ( !TextUtils.isEmpty(commentsCon.getRetime()) ) {
			holder.retime.setText(commentsCon.getRetime());
		} else {
			holder.retime.setText("");
		}
		
		return view;
	}
	
	/**
	 * 设置评分星级
	 * 
	 * @param value
	 */
	private void setScoreLevel(float value, ImageView start) {
		if (2.0 >= value) {
			start.setBackgroundResource(R.raw.start1);
		} else if (4.0 >= value) {
			start.setBackgroundResource(R.raw.start2);
		} else if (6.0 >= value) {
			start.setBackgroundResource(R.raw.start3);
		} else if (8.0 >= value) {
			start.setBackgroundResource(R.raw.start4);
		} else if (10.0 >= value) {
			start.setBackgroundResource(R.raw.start5);
		}
	}
	
	
	static class ViewHolder {
		public CircleImageView uPic;
		public TextView uName;
		public TextView uLevel;
		public TextView comments;
		public TextView cTime;
		public ImageView grade;
		public TextView sendTimes;
		public TextView retime;
		public TextView reComments;
		public RelativeLayout layout;
	}

}
